package sevensoft.gz031.demos.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import sevensoft.gz031.demos.web.entity.Admin;
import sevensoft.gz031.demos.web.mapper.AdminMapper;
import sevensoft.gz031.demos.web.service.AdminService;
import sevensoft.gz031.demos.web.utils.JwtUtil;

import java.util.Objects;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(Admin admin) {
        LambdaQueryWrapper<Admin> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Admin::getUsername,admin.getUsername()).eq(Admin::getPassword,admin.getPassword());
        Admin login = adminMapper.selectOne(lqw);
        if (null!=login){
            String token = JwtUtil.sign(login.getId(), null);
//            redisTemplate.opsForHash().put(login.getId(),"token",token);
            return token;
        }
        return null;
    }

    @Override
    public Admin getInfo(String token) {
        LambdaQueryWrapper<Admin> lqw=new LambdaQueryWrapper<>();
        String id = JwtUtil.getId(token);
        lqw.eq(Admin::getId,id);
        return adminMapper.selectOne(lqw);
    }

    @Override
    public Boolean logout(String token) {
        return redisTemplate.delete(Objects.requireNonNull(JwtUtil.getId(token)));
    }
}
